
from constants import *

from google.appengine.ext import db


class Tenants(db.Model):
    idNo = db.StringProperty()
    firstName = db.StringProperty()
    surName = db.StringProperty()
    phoneNo = db.StringProperty()
    email = db.StringProperty()
    dateReg = db.DateTimeProperty(auto_now_add=True)

    @staticmethod
    def Create(idNo, firstName, surName, phoneNo):
        return Tenants(idNo=idNo, firstName=firstName, surName=surName, phoneNo=phoneNo)
 
class User(db.Model):
    username = db.StringProperty(required=True)
    password = db.StringProperty(required=True)
    phoneNo = db.StringProperty(required=True)
    role = db.StringProperty(required=True, choices=[USER_ADMIN, USER_LANDLORD, USER_TENANT])
    dateReg = db.DateTimeProperty(auto_now_add=True)

    @staticmethod
    def Create(username, phoneNo, password, role):
        if User.Exists(phoneNo, username):
            return None
        return User(phoneNo=phoneNo, username=username, password=password, role=role)

    @staticmethod
    def Exists(phoneNo, username):
        return User.all().filter("phoneNo =", phoneNo).count() > 0 or User.all().filter("username =", username).count() > 0

    @staticmethod
    def Validate(username, password):
        return User.all().filter("username =", username).filter("password =", password).get() or User.all().filter("phoneNo =", username).filter("password =", password).get()


class Plot(db.Model):
    plotName = db.StringProperty()
    location = db.StringProperty()
    county = db.StringProperty()
    owner = db.ReferenceProperty(User)

    @staticmethod
    def Create(plotName, location, county, owner):
        return Plot(plotName=plotName, location=location, county=county, owner=owner)

    @staticmethod
    def GetPlotsByOwner(owner):
        return Plot.all().filter("owner =", owner).order("location")#the space between ownner and = is compulsory

    def vacantCount(self):
        return self.house_set.filter("occupancyStatus =", HOUSE_VACANT).count()

    def occupiedCount(self):
        return self.house_set.filter("occupancyStatus =", HOUSE_OCCUPIED).count()

    def houseCount(self):
        return self.house_set.count()


class HouseType(db.Model):
    houseType = db.StringProperty()
    
    @staticmethod
    def Create(houseType):
        return HouseType(houseType=houseType)

    @staticmethod
    def GetHouseTypes():
        return HouseType.all().order("houseType")
    
    
class Policy(db.Model):
    rent = db.IntegerProperty()
    plot = db.ReferenceProperty(Plot)
    houseType = db.ReferenceProperty(HouseType)
    penaltyRate = db.StringProperty()
    payDeadline = db.IntegerProperty()
    depositRequired = db.IntegerProperty()
    noticeRequired = db.IntegerProperty()

    @staticmethod
    def Create(rent, plot, houseType, penaltyRate, payDeadline, depositRequired, noticeRequired):
        return Policy(rent=rent, plot=plot, houseType=houseType, penaltyRate=penaltyRate, payDeadline=payDeadline, depositRequired=depositRequired, noticeRequired=noticeRequired)

    @staticmethod
    def GetPlotPolicy(plot):
        return Policy.all().filter("plot =", plot).order("rent")

class House(db.Model):
    houseNo = db.StringProperty()
    houseType = db.StringProperty()
    occupancyStatus = db.StringProperty(default=HOUSE_VACANT, choices=[HOUSE_VACANT, HOUSE_OCCUPIED])
    plot = db.ReferenceProperty(Plot)    
    
    @staticmethod
    def Create(houseNo, houseType, occupancyStatus, plot):
        return House(houseNo=houseNo, houseType=houseType, occupancyStatus=occupancyStatus, plot=plot)

    @staticmethod
    def GetHousesByPlot(plot):
        return House.all().filter("plot =", plot).order("houseNo")#the space between ownner and = is compulsory

    @staticmethod
    def GetOccupancyStatus(plot):
        vacant= "VACANT"
        return House.all().filter("occupancyStatus =", vacant).order("houseNo")
    
    @staticmethod
    def NoOfVacantHouse(plot):
        vacant= "VACANT"
        return House.all().filter("occupancyStatus =", vacant).count()





class HouseRent(db.Model):
    tenant = db.ReferenceProperty(User)
    house = db.ReferenceProperty(House)
    startDate = db.DateTimeProperty()
    endDate = db.DateTimeProperty()

    
class Payment(db.Model):
    paid_by = db.ReferenceProperty(User)
    amountRecieved = db.IntegerProperty()
    datePayed = db.DateTimeProperty(auto_now_add=True)

class Cost(db.Model):
    hseNo = db.ReferenceProperty(House)
    costSource = db.StringProperty()
    Amount = db.IntegerProperty()
    description = db.StringProperty()